/*
 * timer.c
 *
 *  Created on: 03-11-2012
 *      Author: Skeith
 */

#include "timer.h"

void T1Start(tU32 time_ms)
{
	TIMER1_TCR = 0x02;          //stop and reset timer
	TIMER1_PR  = 0x00;          //set prescaler to zero
	TIMER1_MR0 = time_ms * ((CRYSTAL_FREQUENCY * PLL_FACTOR)/ (1000 * VPBDIV_FACTOR));
	TIMER1_IR  = 0xff;          //reset all interrrupt flags
	TIMER1_MCR = 0x04;          //stop timer on match
	TIMER1_TCR = 0x01;          //start timer
}

void T1Stop()
{
	TIMER1_TCR = 0x02;          //stop and reset timer
}

tU32 T1ElapsedTime()
{
	tU32 ret;

	ret = TIMER1_TC;

	return ret;
}

tU32 T1ElapsedTimeMs()
{
	tU32 ret;

	ret = (TIMER1_TC) / ((CRYSTAL_FREQUENCY * PLL_FACTOR)/ (1000 * VPBDIV_FACTOR));

	return ret;
}

tBool T1Stopped()
{
	return (TIMER1_TCR & 0x01);
}
